package com.yuanfeng.base.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.base.dto.ManageFormatDeteteDTO;
import com.yuanfeng.base.dto.ManageGoodsFormatQueryDTO;
import com.yuanfeng.base.dto.MerchantGoodsFormatDTO;
import com.yuanfeng.base.entity.BaseFormatEntity;
import com.yuanfeng.base.mapper.BaseFormatMapper;
import com.yuanfeng.base.service.BaseFormatService;
import com.yuanfeng.commoms.aspect.UserPermissionsResult;
import com.yuanfeng.commoms.aspect.UserService;
import com.yuanfeng.commoms.config.PermissionsConstant;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.vo.goods.ManageGoodsFormatResultVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *
 */
@Service
public class BaseFormatServiceImpl extends ServiceImpl<BaseFormatMapper, BaseFormatEntity>
        implements BaseFormatService {

    @Override
    public ResponseResult queryGoodsFormat(ManageGoodsFormatQueryDTO param) {
        // 权限判断
        UserPermissionsResult userPermissionsResult = UserService
                .judgePermissions(PermissionsConstant.SERLLER_SHOP_SETSHOP, param.getToken());

        if (!userPermissionsResult.getFlag()) {
            return ResponseResult.fail(10086, "没有权限");
        }
        // 查询关联板式
        param.setShopId(userPermissionsResult.getShopId());
        List<ManageGoodsFormatResultVO> goodsFormats = this.baseMapper.selectByShopId(param);
        return ResponseResult.success(goodsFormats);
    }

    @Override
    @Transactional
    public ResponseResult updateGoodsFormat(MerchantGoodsFormatDTO param) {
        // 权限判断
        UserPermissionsResult userPermissionsResult = UserService
                .judgePermissions(PermissionsConstant.SERLLER_SHOP_SETSHOP, param.getToken());
        if (!userPermissionsResult.getFlag()) {
            return ResponseResult.fail(10086, "没有权限");
        }
        BaseFormatEntity baseFormatEntity = new BaseFormatEntity();
        baseFormatEntity.setId(param.getId());
        baseFormatEntity.setName(param.getName());
        baseFormatEntity.setShopId(Integer.valueOf(userPermissionsResult.getShopId()));
        baseFormatEntity.setContent(param.getContent());
        baseFormatEntity.setPosition(param.getPosition());
        // 新增或更新关联板式
        if (param.getId() == null) {
            this.baseMapper.insert(baseFormatEntity);
        } else {
            this.baseMapper.updateById(baseFormatEntity);
        }
        return ResponseResult.success();
    }

    @Override
    public ResponseResult deleteByFormatId(ManageFormatDeteteDTO param) {
        // 权限判断
        UserPermissionsResult userPermissionsResult = UserService
                .judgePermissions(PermissionsConstant.SERLLER_SHOP_SETSHOP, param.getToken());

        if (!userPermissionsResult.getFlag()) {
            return ResponseResult.fail(10086, "没有权限");
        }
        // 删除联板式
        this.baseMapper.deleteById(param.getFormatId());
        return ResponseResult.success();
    }
}




